package com.business.dynamic.controller;

import com.alibaba.fastjson.JSONObject;
import com.business.common.dto.R;
import com.business.common.enums.ErrorEnum;
import com.business.common.exception.CustomException;
import com.business.dynamic.service.DynamicCollectionService;
import com.business.user.annotation.FrequentRequest;
import com.business.user.annotation.TokenPermission;
import com.business.user.entity.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户收藏表: 后端controller类
 *
 * @author chenjiehong
 * @date 2019-12-02 17:55:03
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/collection")
@TokenPermission
public class DynamicCollectionController {

    @Autowired
    private DynamicCollectionService dynamicCollectionService;

    /**
     * 添加收藏
     *
     * @param params
     * @param userDto
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @FrequentRequest(15)//控制频繁操作 一分钟内可以操作15次
    public R add(@RequestBody JSONObject params, UserDto userDto) {
        Integer dynamicId = params.getInteger("dynamicId");
        if (dynamicId == null) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "dynamicId");
        }
        return R.success(dynamicCollectionService.add(dynamicId, userDto.getId()));
    }


    /**
     * 删除收藏
     *
     * @param params
     * @param userDto
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    @FrequentRequest(15)//控制频繁操作 一分钟内可以操作15次
    public R delete(@RequestBody JSONObject params, UserDto userDto) {
        Integer dynamicId = params.getInteger("dynamicId");
        if (dynamicId == null) {
            throw new CustomException(ErrorEnum.PARAMS_LOSE, "dynamicId");
        }
        return R.success(dynamicCollectionService.delete(dynamicId, userDto.getId()));
    }

}